package com.example.administrator.nurse.widget;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * Created by Administrator on 2015/11/26.
 */

public class dialogHelper {

    public static void showDialog(Context context,int layoutId,operationListener thisOpListener){

        final AlertDialog dialog = new AlertDialog.Builder(context).create();

        View view = LayoutInflater.from(context).inflate(layoutId,null,false);

        if(thisOpListener!=null){
            thisOpListener.operation(view,dialog);
        }else{
            return;
        }

        Window window = dialog.getWindow();
        dialog.show();
        window.setContentView(view);
        window.setGravity(17);
    }

    public static void showEditableDialog(
            Context context,
            int layoutId,
            operationListener thisOpListener
    ){
        final AlertDialog dlg = new AlertDialog.Builder(context).create();

        View view = LayoutInflater.from(context).inflate(layoutId,null,false);

        if(thisOpListener!=null){
            thisOpListener.operation(view,dlg);
        }else{
            return;
        }

        Window localWindow = dlg.getWindow();

        localWindow.getAttributes();
        dlg.show();
        localWindow.setContentView(view);
        WindowManager.LayoutParams params = localWindow.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;//如果不设置,可能部分机型出现左右有空隙,也就是产生margin的感觉
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.softInputMode = WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE;//显示dialog的时候,就显示软键盘
        params.flags = WindowManager.LayoutParams.FLAG_DIM_BEHIND;//就是这个属性导致不能获取焦点,默认的是FLAG_NOT_FOCUSABLE,故名思义不能获取输入焦点,
        params.dimAmount=0.5f;//设置对话框的透明程度背景(非布局的透明度)
        localWindow.setAttributes(params);
        localWindow.setGravity(17);
    }

    public interface operationListener{
        void operation(View view, final AlertDialog dialog);
    }


    public static void cancel(final Activity activity,String str, final finishListener finishLi){
        final CharSequence[] items = {"确认","取消"};
        final AlertDialog dlg = new AlertDialog.Builder(activity).setTitle(str).setItems(items,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int item) {
                        if (item == 1) {
                            dialog.cancel();
                        } else {
                            if(finishLi!=null){
                                finishLi.finishOp();
                            }
                        }
                    }
                }).create();
        dlg.show();
    }

    public interface finishListener{
        void finishOp();
    }

    /*public static void showDialog(
            int layoutId,final Activity activity,final operationListener thisOpListener,final finishListener finish,String titleText) {
        AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.Dialog);
        final View view = LayoutInflater.from(activity).inflate(layoutId, null);
        TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
        tv_title.setText(titleText);
        builder.setView(view);
        final AlertDialog alert = builder.create();
        view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.dismiss();
                if (finish!=null) {
                    finish.finishOp();
                }
            }
        });
        view.findViewById(R.id.sure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (thisOpListener != null) {
                    thisOpListener.operation(view, alert);
                }
            }
        });
        alert.show();
    }*/

}
